/*****************************************************
 * created by lei.he on 2020-07-27 for 项目启动文件
 *****************************************************/

const http = require('http');

const app = http.createServer((req,res) => {
    console.log(req.method)
    console.log(req.url)
    console.log('<<<<<<<<<<<<<<<<<<<<<<')
    handleRusult(req.url,req.method,res);
});

/**
 * 
 * @param {string} url                  //  请求地址 
 * @param {string} method               //  请求方法
 * @param {ServerResponse} res          //  res服务端响应体
 */

const handleRusult = (url,method,res) => {
    if (method === 'GET'){
        if (url === '/user/login') {
            res.write(JSON.stringify({code:200,message:'',data : {userId : 'admin',userName : '管理员'}}))
        } else {
            res.write(JSON.stringify({code:404,message : 'not found'}))
        }
    } else {
        res.write(JSON.stringify({code:400,message : 'not suports this methods'}));
    }
    res.end();
}

const SERVER_PORT = 9876 ;

app.listen(SERVER_PORT , () => {
    console.log(`server started on port : ${SERVER_PORT}`);
})